'use strict';
const fs = require('fs');
const path  = require('path');
const jwt = require('jsonwebtoken') //引入jsonwebtoken

module.exports = {
  loginToken(data, expires = this.app.config.timeStamp) {
    const exp = Math.floor(Date.now() / 1000) + expires
    const cert = fs.readFileSync(path.join(__dirname, '../public/rsa_private_key.pem')) // 私钥，看后面生成方法
    const token = jwt.sign({ data, exp }, cert, { algorithm: 'RS256' })
    return token
  },
  success(ctx,msg,data){  //成功
    ctx.body={
      code:0,
      msg,
      data,
    };
  },
  error(ctx,msg,data){   //失败
    ctx.body = {
      code:1,
      msg,
      data,
    }
  },
  successTotal(ctx,msg,data,total){  //返回分页
    ctx.body={
      code:0,
      total,
      msg,
      data,
    };
  }
}